package cn.flying.cloud.tool.ops.instance.service.impl;

import java.io.Serializable;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.tool.ops.instance.entity.InstanceInfo;
import cn.flying.cloud.tool.ops.instance.mapper.InstanceInfoMapper;
import cn.flying.cloud.tool.ops.instance.service.InstanceInfoService;

/**
 * 实例信息表 服务实现类
 *
 * @author admin
 * @since 2025-04-25 23:11:44
 */
@Service
public class InstanceInfoServiceImpl extends ServiceImpl<InstanceInfoMapper, InstanceInfo> implements InstanceInfoService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 查询总数
     *
     * @param instanceInfo
     * @return
     */
    @Override
    public long countObj(InstanceInfo instanceInfo) {
        return this.count(getLambdaQueryWrapper(instanceInfo));
    }

    /**
     * 分页查询实例信息表数据
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<InstanceInfo> getPageList(PageRequest<InstanceInfo> request) {
        InstanceInfo param = request.getParam();
        LambdaQueryWrapper<InstanceInfo> wrapper = getLambdaQueryWrapper(param);
        Page<InstanceInfo> page = this.page(new Page<>(request.getPageNum(), request.getPageSize()), wrapper);
        return new PageResponse<>(page.getTotal(), (int) page.getCurrent(), page.getRecords());
    }

    /**
     * 构建查询对象
     *
     * @param param
     * @return
     */
    private LambdaQueryWrapper<InstanceInfo> getLambdaQueryWrapper(InstanceInfo param) {
        LambdaQueryWrapper<InstanceInfo> wrapper = new LambdaQueryWrapper<>();
        if (param != null) {
            if (Ft.String.isNotBlank(param.getServiceCode())) {
                wrapper.eq(InstanceInfo::getServiceCode, param.getServiceCode());
            }
            if (Ft.String.isNotBlank(param.getHost())) {
                wrapper.eq(InstanceInfo::getHost, param.getHost());
            }
            if (Ft.String.isNotBlank(param.getArtifactId())) {
                wrapper.eq(InstanceInfo::getArtifactId, param.getArtifactId());
            }
            if (Ft.String.isNotBlank(param.getGroupId())) {
                wrapper.eq(InstanceInfo::getGroupId, param.getGroupId());
            }
            if (Ft.String.isNotBlank(param.getStatus())) {
                wrapper.eq(InstanceInfo::getStatus, param.getStatus());
            }
        }
        return wrapper;
    }

    /**
     * 列表查询实例信息表数据
     *
     * @param instanceInfo
     * @return
     */
    @Override
    public List<InstanceInfo> getList(InstanceInfo instanceInfo) {
        LambdaQueryWrapper<InstanceInfo> wrapper = new LambdaQueryWrapper<>();
        // wrapper TODO
        return this.list(wrapper);
    }

    /**
     * 查询实例信息表详情数据
     *
     * @param instanceInfo
     * @return
     */
    @Override
    public InstanceInfo getOneObj(InstanceInfo instanceInfo) {
        LambdaQueryWrapper<InstanceInfo> wrapper = new LambdaQueryWrapper<>();
        // wrapper TODO
        return this.getOne(wrapper);
    }

    /**
     * 根据Id查询实例信息表详情数据
     *
     * @param id
     * @return
     */
    @Override
    public InstanceInfo getObjById(Serializable id) {
        return this.getById(id);
    }

    /**
     * 新增实例信息表数据
     *
     * @param instanceInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createObj(InstanceInfo instanceInfo) {
        instanceInfo.setTenantId(Ft.Local.TenantId());
        return this.save(instanceInfo);
    }

    /**
     * 更新实例信息表数据
     *
     * @param instanceInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateObj(InstanceInfo instanceInfo) {
        return this.saveOrUpdate(instanceInfo);
    }

    /**
     * 删除实例信息表数据
     *
     * @param instanceInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteObj(InstanceInfo instanceInfo) {
        LambdaQueryWrapper<InstanceInfo> wrapper = new LambdaQueryWrapper<>();
        // wrapper TODO
        return this.remove(wrapper);
    }

    /**
     * 根据Id删除实例信息表数据
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Serializable id) {
        return this.removeById(id);
    }
}
